package com.example.sun.testread;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.physicaloid.lib.Physicaloid;
import com.physicaloid.lib.usb.driver.uart.UartConfig;
import com.physicaloid.lib.usb.driver.uart.ReadLisener;

import java.security.Timestamp;
import java.text.SimpleDateFormat;

public class MainActivity extends AppCompatActivity {

    Button OpenButton, CloseButton;
    TextView displayText;
    long a;
    Physicaloid mPhysicaloid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        OpenButton = (Button) findViewById(R.id.button);
        CloseButton = (Button) findViewById(R.id.button2);
        displayText = (TextView) findViewById(R.id.textView);
        mPhysicaloid =new Physicaloid(this);
        OpenButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mPhysicaloid.open()) { // default 9600bps
                    UartConfig uartConfig = new UartConfig(115200, UartConfig.DATA_BITS8, UartConfig.STOP_BITS1, UartConfig.PARITY_NONE, false, false);
                    mPhysicaloid.setConfig(uartConfig);
                    mPhysicaloid.addReadListener(new ReadLisener() {
                        String readStr;

                        // callback when reading one or more size buffer
                        @Override
                        public void onRead(int size) {
                            byte[] buf = new byte[128];

                            mPhysicaloid.read(buf, 128);
                            readStr = new String(buf);
                            a= System.currentTimeMillis();
                            Log.e("test",readStr);
                            String atest=a+" ";
                            Log.e("test",atest);

                            //Log.d("test",System.currentTimeMillis());
                            // UI thread
                            //displayText.setText(readStr);
                        }
                    });

                }

            }        });


        CloseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mPhysicaloid.close()) {
                    mPhysicaloid.clearReadListener();
                }
            }
        });

    }






}
